<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        class="serch-btn"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- 文章频道 -->
    <van-tabs class="channel-tabs" v-model="active">
      <van-tab
      class="tab-item"
      :title="channel.name"
      v-for="channel in channels"
      :key="channel.id">
        <!-- 文章列表 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <div
      slot="nav-right"
      class="wap-nav-placeholder"></div>
      <div
      slot="nav-right"
      @click="isChannelEditShow = true"
      class="wap-nav-warp">
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup
    v-model="isChannelEditShow"
    position="bottom"
    class="channel-edit-popup"
    closeable
    close-icon-position="top-left"
     get-container="body"
     style="height: 100%" >
      <channel-edit
      :user-channels="channels"
      :active="active"
      @close="isChannelEditShow = false"
      @update-active="active = $event"/>
     </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'HomeIndex',
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      let channels = []
      if (this.user) {
        // 线上用户频道数据
        const { data } = await getUserChannels()
        channels = data.data.channels
        console.log('已登录--获取线上用户频道数据')
      } else {
        // 没有登录 判断是否有本地存储的数据
        const localChannels = getItem('user-channels')
        if (localChannels) {
          // 有本地存储的数据
          console.log('有本地存储的数据--读取localStorage')
          channels = localChannels
        } else {
          console.log('匿名用户---获取推荐的频道列表')
          // 用户没有登录 也没有本地存储的频道 就获取默认推荐的
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }
      // 把处理完的数据 放到data中
      this.channels = channels
      // 请求获取频道数据
      // const { data } = await getUserChannels()
      // this.channels = data.data.channels
    }
    // onUpdateActive (index) {
    //   this.active = index
    // }
  }
}
</script>

<style lang="less" scoped>
  .home-container {
    /deep/ .van-nav-bar__title{
      max-width: none;
    }
    .serch-btn {
      width: 277px;
      height: 32px;
      background: #5babfb;
      border: none;
      .van-icon {
        font-size: 16px;
      }
      .van-button__text {
        font-size: 14px;
      }
    }
    .channel-tabs {
      /deep/ .van-tab {
        border-right: 1px solid #edeff3;
        border-bottom: 1px solid #edeff3;
      }
      /deep/ .van-tabs__line {
        bottom: 20px;
        width: 15px !important;
        height: 3px;
        background: #3296fa;
      }
    }
    .wap-nav-placeholder {
      width: 33px;
      flex-shrink: 0;
    }
    .wap-nav-warp {
      position: fixed;
      right: 0;
      width: 33px;
      height: 43px;
      background:#fff;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0.9;
      .van-icon {
        font-size: 24px;;
      }
    }
    &:before {
      content: '';
      width: 1px;
      height: 43px;
      background: url("./line.png") no-repeat;
      background-size: contain;
      position: absolute;
      left: 0;
      top: 0;
    }
  }
</style>
